<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/12/29
 * Time: 13:45
 */

namespace bc\model;

use bc\ar\MActiveRecord;
class OrderbindMode extends MActiveRecord
{
    /**
     *
     */
    public static function tableName()
    {
        return '{{%order_bind}}';
    }

    /*
     * 根据userid获取用户的采样盒信息
     * */
    public function getBindInfoFromUserid($userid,$productid,$order_id)
    {
        if(empty($order_id)){
            $params=array(
                //"bc_order.user_id" => $userid,
                "bc_order_bind.product_id" =>  $productid,
                "bc_order_bind.user_id" =>  $userid
            );
        }else{
            $params=array(
                //"bc_order.user_id" => $userid,
                "bc_order_bind.product_id" =>  $productid,
                "bc_order_bind.user_id" =>  $userid,
                "bc_order_bind.order_id" =>  $order_id
            );
        }

        $row = $this->find()->select($this->columns)->leftJoin('bc_order', 'bc_order.order_id = bc_order_bind.order_id')->where($params);
        if($row)
            return $row->asArray()->all();

        return null;
    }

    public function getRow($params = array())
    {
        $row = $this->find()->select($this->columns)
            ->leftJoin('bc_order', 'bc_order_bind.order_id = bc_order.order_id')
            ->leftJoin('bc_order_detail', 'bc_order.order_id = bc_order_detail.order_id')
            ->leftJoin('bc_product', 'bc_product.product_id = bc_order_detail.product_id')
            ->leftJoin('bc_address', 'bc_address.id = bc_order.address_id')
            ->leftJoin('bc_wx_user', 'bc_wx_user.id = bc_order.user_id')
            ->leftJoin('ecs_users', 'bc_wx_user.openid = ecs_users.wxid')
            ->leftJoin('bc_order_status', 'bc_order_status.order_status_id = bc_order.order_status')->where($params);

        return $row->asArray()->all();
    }


    public function getRowP($params = array())
    {
        return parent::getRow($params); // TODO: Change the autogenerated stub
    }

    /**
     * 
     * @param unknown $params
     */
    public function getBindInfo($params = null)
    {
    	$ar = $this->find()->select($this->columns)
    			->leftJoin('bc_order', 'bc_order_bind.order_id = bc_order.order_id');
    	if(!is_null($params))
    		$ar->where($params);
    	return $ar->asArray()->all();
    	
    }

    //根据订单ID获取订单下样本详细信息
    public function getBindDetailedInfoFromOrderId($orderid = null)
    {
        if(empty($orderid)){
            return array();
        }

        $ar = $this->find()->select($this->columns)
            ->leftJoin('bc_express', 'bc_order_bind.bind_id = bc_express.bind_id')
            ->leftJoin('bc_express_company', 'bc_express.company_id = bc_express_company.company_id')
            ->leftJoin('bc_item_data', 'bc_order_bind.bind_id = bc_item_data.bind_id')
            ->leftJoin('bc_item', 'bc_item.id = bc_item_data.item_product_id')
            ->where(['bc_order_bind.order_id'=>$orderid]);
//        echo $ar->createCommand()->getRawSql();
        $SrcData = $ar->asArray()->all();

        //去除姓名，性别，年龄，快递号，样本号
        $returnData = array();
        foreach($SrcData as $key => $value)
        {
            if($value['item_report_key'] == 'info_name' ){
                $returnData[$value['order_bind_id']]['info_name'] = $value['data_item_value'];
                $returnData[$value['order_bind_id']]['order_bind_no'] = $value['order_bind_no'];
                $returnData[$value['order_bind_id']]['order_bind_status'] = $value['order_bind_status'];
                $returnData[$value['order_bind_id']]['order_bind_status_name'] = static::$samplestatus[$value['order_bind_status']];
                $returnData[$value['order_bind_id']]['express_tracking_no'] = $value['express_tracking_no'];
                $returnData[$value['order_bind_id']]['company_company_name'] = $value['company_company_name'];
            }
            elseif( $value['item_report_key'] == 'info_sex'){
                $returnData[$value['order_bind_id']]['info_sex'] = static::$Sex[$value['data_item_value']] ;
            }
            elseif($value['item_report_key'] == 'info_birthday')  {
                $returnData[$value['order_bind_id']]['item_report_key'] = $value['data_item_value'];
            }
        }
//        var_dump($returnData) ;
//        die();
        return $returnData;
    }

}